package com.ls;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 插旗法
 * @author : 梁苏
 * @date : 2023/2/5 19:57
 **/
public class InsertFlag {

    Map<Integer, Integer> map = new HashMap<>();

    public static void main(String[] args) {
        // 2-5 3-10

        // 2+   3+  6-   11-

        InsertFlag insertFlag = new InsertFlag();
        insertFlag.add(2, 5);
        insertFlag.add(3, 10);
        insertFlag.print();
    }

    public void add(Integer start, Integer end){
        Integer count = map.get(start);
        if(count == null){
            map.put(start, 1);
        }else{
            map.put(start, count + 1);
        }

        Integer endCount = map.get(end+1);
        if(endCount == null){
            map.put(end + 1, -1);
        }else{
            map.put(end + 1, endCount - 1);
        }
    }

    public void print(){
        ArrayList<Integer> indexs = new ArrayList<>(map.keySet());
        Collections.sort(indexs);

        // 1: 2-3  2:3-5 1: 6-10 0:10-
        int cur = 0;
        int start = -1;
        for (Integer index : indexs) {
            Integer num = map.get(index);
            cur += num;

            if(start == -1){
                start = index;
                System.out.print(cur + ": [" + start);
            }else{
                System.out.println("," + (index-1) + "]");
                start = index;
                System.out.print(cur + ": [" + start);
            }

        }
        System.out.println();

    }

}
